package com.huang.foundation.controller;

import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import com.huang.foundation.entity.em.SmsType;
import com.huang.foundation.entity.request.SendSmsRequest;
import com.huang.foundation.service.SmsService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author HuangShen
 * @Date 2022/2/23 23:43
 * @Describe
 */
@RestController
@RequestMapping("/public")
@Api("短信相关接口")
public class SmsController {

    @Resource(name = "tencentCloudSmsService")
    private SmsService smsService;

    @PostMapping("/sendSms")
    public HStringResult<String> sendSms(@RequestBody SendSmsRequest sendSmsRequest) {
        // 参数校验
        try {
            sendSmsRequest.check();
            // 目前仅支持大陆手机号
            SmsType smsType = SmsType.getSmsTypeByCode(Integer.valueOf(sendSmsRequest.getType()));
            if (Objects.isNull(smsType)) {
                return ResultBuilder.failHSingle("消息类型不存在");
            }
            if (this.smsService.sendSms(smsType, sendSmsRequest.getTel())) {
               return ResultBuilder.successHSingle();
            }
        }catch (RuntimeException r){
            return ResultBuilder.failHSingle(r.getMessage());
        }
        return ResultBuilder.failHSingle();
    }

}
